package com.hzx.demo.helper;

import com.hzx.demo.util.HZXLog;
import com.hzx.demo.util.IOUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * logcat的抓取器,
 * 使用此类需要相应的权限
 * @note 此类仿照acra的LogCatCollector, 一切智慧尽归原作者
 * Created by ldkxingzhe on 2016/5/19.
 */
public class LogCatCollector {
    @SuppressWarnings("unused")
    private static final String TAG = "LogCatCollector";

    private static final int DEFAULT_TAIL_COUNT = 100;

    public String collectLog(){
        int myPid = android.os.Process.myPid();
        List<String> commandLine = new ArrayList<>();
        commandLine.add("logcat");
        commandLine.add("-v");
        commandLine.add("time");
        commandLine.add("-d");
        commandLine.add("-t");
        commandLine.add("200");
        try {
            java.lang.Process process = Runtime.getRuntime().exec(commandLine.toArray(new String[commandLine.size()]));
            HZXLog.v(TAG, "process is %s.", process);
            return IOUtil.streamToString(process.getInputStream());
        } catch (IOException e) {
            HZXLog.e(TAG, e.getMessage(), e);
            return e.getMessage();
        }
    }
}
